<?php

require_once './core/constants.php';

/**
*	Database connection
*/


function dbConnect($db_host, $db_user, $db_pass, $db_name){
	
	global $link;
	$link = mysql_connect($db_host, $db_user, $db_pass);
	if (!$link){
		return mysql_error();
	}

	$selectDb = mysql_select_db($db_name);

	if (!$selectDb) {
		return mysql_error();
	}

	return "ok";
}

function executeSQL($sql){
	dbConnect(getValueInIniFile("dbhost"), getValueInIniFile("dbuser"), getValueInIniFile("dbpwd"), getValueInIniFile("dbname"));
	global $link;
	$sqlReturn = mysql_query($sql, $link);
	dbDisconnect();
	return $sqlReturn;
}



function dbDisconnect(){
	global $link;
	mysql_close($link);
}



/**
*	Ini file
*/


function readIni($path) {
	$conf = parse_ini_file($path, true);
	print_r($conf);
}

function write_ini_file($assoc_arr) { 
    $content = ""; 
   
    foreach ($assoc_arr as $key=>$elem) { 
        if(is_array($elem)) 
        { 
            for($i=0;$i<count($elem);$i++) 
            { 
                $content .= $key."[] = \"".$elem[$i]."\"\n"; 
            } 
        } 
        else if($elem=="") $content .= $key." = \n"; 
        else $content .= $key." = \"".$elem."\"\n"; 
    } 
 

    if (!$handle = fopen(CONFIG_INI, 'w')) { 
        return false; 
    } 
    if (!fwrite($handle, $content)) { 
        return false; 
    } 
    fclose($handle); 
    return true; 
}

function getValueInIniFile($key) {
	global $conf;

	if (!isset($conf))
		$conf = parse_ini_file(CONFIG_INI, false);

	return $conf[$key]; 
}


function removeIniFile() {
	unlink(CONFIG_INI);
}



/**
*	Config
*/

function isConfigured(){
	return file_exists(CONFIG_INI);
}



?>